<?php
class Datos extends CI_Controller{

    var $url ='empresa/datos/';
    var $pre ='empresa-datos';

    function __construct(){
        parent::__construct();
        $this->lang->load('empresa/datos');
        $this->load->library('form_validation');
    }

    function principal($id=false){
		$data=array();
		if(!$id)
		$id  = $this->session->userdata("id_usuario");
		
		$rol = $this->util->dameval("SELECT rol FROM usuario WHERE id=$id");
		/*SI EL ROL ES EMPRESA SE TRAE LOS DATOS DE LA EMPRESA*/
		if($rol==4){
			$query="
			SELECT a.*,b.nombre t_sector_emp,c.nombre t_poblacion,d.nombre t_provincia,e.nombre t_pais 
			FROM empresas a 
			INNER JOIN usuario f ON a.id_usuario=f.id
			INNER JOIN sectores_emp b ON a.id_sector_emp=b.id 
			INNER JOIN poblacion c ON a.id_poblacion=c.id 
			INNER JOIN provincia d ON c.id_provincia=d.id 
			INNER JOIN pais e ON d.id_pais=e.id 
			WHERE f.id=$id
			";
			$data['datos']=$this->util->damerow($query);
		}
		/**/
		
		/*-SI NO CARGO LA DATA DE LA EMPRESA INICIALIZA EN BLANCO LAS VARIABLES QUE SE VAN A USAR EN EL VIEW-*/
		if(count($data['datos'])==0){
			$query="
			SELECT a.*,'' t_sector_emp,'' t_poblacion,'' t_provincia,'' t_pais 
			FROM empresas a 
			WHERE 1=2
			";
			$query=$this->db->query($query);
			
			foreach($query->list_fields() as $value)
			$data['datos'][$value]='';
		}
		/*--*/
		
		if($rol==2)
		$data['datos']['id_agente']=$id;
		
		$this->load->view('empresa-datos', $data);
	}

	function guardar(){
		$msj  ='';
		$error='';
		$idk  ='id';
		$table='empresas';
		$d    = $this->input->post();
		unset($d['t_sector_emp']);
		unset($d['t_poblacion'] );
		unset($d['t_provincia'] );
		unset($d['t_pais']      );

		//$this->form_validation->set_rules('cif'            , $this->lang->line("F_CIF"                ) , implode('|',array('required','max_length[15]' ,'exact_length[9]'  ,'date_is_valid'                               )));
		
		$this->form_validation->set_rules('cif'            , $this->lang->line("F_CIF"                ) , implode('|',array('required'                   ,'exact_length[9]'                                                 )));
		$this->form_validation->set_rules('nif'            , $this->lang->line("F_NIF"                ) , implode('|',array(                              'exact_length[9]'                                                 )));
		$this->form_validation->set_rules('nombre_com'     , $this->lang->line("F_NOMBRE_COM"         ) , implode('|',array('required' ,'max_length[255]'                                                                   )));
		$this->form_validation->set_rules('razon_soc'      , $this->lang->line("F_RAZON_SOC"          ) , implode('|',array('required' ,'max_length[255]'                                                                   )));
		
		$c=$this->util->dameval("SELECT COUNT(*) FROM sectores_emp WHERE id=".$this->db->escape($d['id_sector_emp']));
		if($c==0)
		$error.=$this->lang->line("F_E_ID_SECTOR_EMP"       ).br();
		
		//$this->form_validation->set_rules('actividad'      , $this->lang->line("F_ACTIVIDAD"          ) , implode('|',array('required' ,'max_length[255]'                                                                   )));
		
		$c=$this->util->dameval("SELECT COUNT(*) FROM poblacion WHERE id=".$this->db->escape($d['id_poblacion']));
		if($c==0)
		$error.=$this->lang->line("F_E_POBLACION"       ).br();
		
		//$this->form_validation->set_rules('direccion'      , $this->lang->line("F_DIRECCION"          ) , implode('|',array('required' ,'max_length[255]'                                                                   )));
		$this->form_validation->set_rules('via'            , $this->lang->line("F_VIA"                ) , implode('|',array(            'max_length[100]'                                                                   )));
		$this->form_validation->set_rules('nombre_via'     , $this->lang->line("F_NOMBRE_VIA"         ) , implode('|',array(            'max_length[255]'                                                                   )));
		$this->form_validation->set_rules('numero_via'     , $this->lang->line("F_NUMERO_VIA"         ) , implode('|',array(            'max_length[50]'                                                                    )));
		$this->form_validation->set_rules('codigo_postal'  , $this->lang->line("F_CODIGO_POSTAL"      ) , implode('|',array(            'max_length[8]'                                            ,'is_natural'            )));
		$this->form_validation->set_rules('telefono2'      , $this->lang->line("F_TELEFONO2"          ) , implode('|',array(            'max_length[20]'                                                                    )));
		$this->form_validation->set_rules('fax'            , $this->lang->line("F_FAX"                ) , implode('|',array(            'max_length[20]'                                                                    )));
		$this->form_validation->set_rules('contacto'       , $this->lang->line("F_CONTACTO"           ) , implode('|',array('required' ,'max_length[200]'                                                                   )));
		$this->form_validation->set_rules('cargo_con'      , $this->lang->line("F_CARGO_CON"          ) , implode('|',array('required' ,'max_length[200]'                                                                   )));
		$this->form_validation->set_rules('telefono'       , $this->lang->line("F_TELEFONO"           ) , implode('|',array('required' ,'max_length[20]'                                                                    )));
		$this->form_validation->set_rules('email_con'      , $this->lang->line("F_EMAIL_CON"          ) , implode('|',array('required' ,'max_length[200]'                                                                   )));
		$this->form_validation->set_rules('movil'          , $this->lang->line("F_MOVIL"              ) , implode('|',array('required' ,'max_length[20]'                                                                    )));

		$this->form_validation->set_error_delimiters('', '</br>');
		$this->form_validation->run();
		$error     .=$this->load->view('validation_errors',array(),true);
		$array_error=$this->form_validation->array_errors();
        
		if(empty($error)){
			$resp            =guardar($table,$d,$idk,$d[$idk]);
			$error          .=$resp['error'];
			$msj            .=$resp['mensaje'];
		}
		echo json_encode(array('error' => $error,'mensaje' => $msj,'id'=>array('key'=>$idk,'value'=>$d[$idk]),'array_error'=>$array_error));
	}
}
